package com.yuwj.chinaa.util;

import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.activity.AccountSettingActivity;
import com.yuwj.chinaa.activity.LoginActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityAppInfo;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.view.DownloadFileProgressDialog;

/**
 * 检查更新帮助类
 */

public class CheckUpdateUtil {
    NiceDialog niceDialogWithUpdateTip;

    /**
     * @param activity
     * @param entityUpdateInfo
     * @param tipType          提示类型,1-APP更新,2-地图更新,3-地图下载(首次安装,没有地图)
     */
    public void showUpdateTipDialog(final AppCompatActivity activity, final EntityAppInfo entityUpdateInfo, final int tipType) {
        if (niceDialogWithUpdateTip == null) {
            niceDialogWithUpdateTip = NiceDialog.init();
            niceDialogWithUpdateTip.setLayoutId(R.layout.content_checkupdate_tipp);

            niceDialogWithUpdateTip.setDimAmount(0.3f)//背景灰暗程度
                    .setMargin(30)
                    .setOutCancel(true)
                    .setShowBottom(false);
        }

        niceDialogWithUpdateTip.setConvertListener(new ViewConvertListener() {
            @Override
            protected void convertView(ViewHolder viewHolder, BaseNiceDialog baseNiceDialog) {
                TextView tvTitle = viewHolder.getView(R.id.tv_title);
                switch (tipType) {
                    case 1:
                        tvTitle.setText("应用更新");
                        break;
                    case 2:
                        tvTitle.setText("地图更新");
                        break;
                    case 3:
                        tvTitle.setText("地图下载");
                        break;
                }
                TextView tvTip = viewHolder.getView(R.id.tv_update_message);
                switch (tipType) {
                    case 1:
                        tvTip.setText(entityUpdateInfo.getContent() + "");
                        break;
                    case 2:
                        tvTip.setText(entityUpdateInfo.getContent() + "");
                        break;
                    case 3:
                        tvTip.setText("检测到地图文件不存在,为保证app正常运行,请先下载地图");
                        break;
                }

                TextView btnConfirm = viewHolder.getView(R.id.tv_confirm);
                TextView btnCancel = viewHolder.getView(R.id.tv_cancel);
                View.OnClickListener onClickListener = new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (v.getId()) {
                            case R.id.tv_confirm:
                                switch (tipType) {
                                    case 1:
                                        if (activity instanceof LoginActivity) {
                                            if (niceDialogWithUpdateTip != null) {
                                                niceDialogWithUpdateTip.dismiss();
                                            }
                                            DownloadFileProgressDialog downloadAppDialog = new DownloadFileProgressDialog(activity);
                                            IPresenter presenter = ((LoginActivity) activity).getPresenter();
                                            presenter.downloadFile(downloadAppDialog, entityUpdateInfo.getDownloadurl(), ConstantParam.DOWNLOAD_FILE_PATH, "GTE_C" + entityUpdateInfo.getVersion() + "_N" + entityUpdateInfo.getVersion() + ".apk");
                                        }
                                        if (activity instanceof AccountSettingActivity) {
                                            if (niceDialogWithUpdateTip != null) {
                                                niceDialogWithUpdateTip.dismiss();
                                            }
                                            DownloadFileProgressDialog downloadAppDialog = new DownloadFileProgressDialog(activity);
                                            IPresenter presenter = ((AccountSettingActivity) activity).getPresenter();
                                            presenter.downloadFile(downloadAppDialog, entityUpdateInfo.getDownloadurl(), ConstantParam.DOWNLOAD_FILE_PATH, "GTE_C" + entityUpdateInfo.getVersion() + "_N" + entityUpdateInfo.getVersion() + ".apk");
                                        }
                                        break;
                                }

                                break;
                            case R.id.tv_cancel:
                                if (niceDialogWithUpdateTip != null) {
                                    niceDialogWithUpdateTip.dismiss();
                                }
                                break;
                        }
                    }
                };
                btnConfirm.setOnClickListener(onClickListener);
                btnCancel.setOnClickListener(onClickListener);
            }
        });

        niceDialogWithUpdateTip.show(activity.getSupportFragmentManager());
    }


}
